/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.bookingmanager.adapters.impl;

import cz.muni.fi.pa165.bookingmanager.adapters.HotelAdapter;
import cz.muni.fi.pa165.bookingmanager.dto.HotelDTO;
import cz.muni.fi.pa165.bookingmanager.entities.Hotel;
import cz.muni.fi.pa165.bookingmanager.exceptions.IllegalOrphanException;
import cz.muni.fi.pa165.bookingmanager.exceptions.NonexistentEntityException;
import cz.muni.fi.pa165.bookingmanager.services.HotelService;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Jiří Holuša
 */
@Stateless
public class HotelAdapterImpl implements HotelAdapter {

    @EJB
    private HotelService hotelService;

    @Override
    public void createHotel(HotelDTO hotel) throws IllegalOrphanException {
        Hotel hotelEntity = AdaptersHelper.hotelDTOToEntity(hotel);

        hotelService.createHotel(hotelEntity);
    }

    @Override
    public void updateHotel(HotelDTO hotel) throws NonexistentEntityException, IllegalOrphanException, Exception {
        Hotel hotelEntity = AdaptersHelper.hotelDTOToEntity(hotel);

        hotelService.updateHotel(hotelEntity);
    }

    @Override
    public void deleteHotel(HotelDTO hotel) throws NonexistentEntityException, IllegalOrphanException {
        Hotel hotelEntity = AdaptersHelper.hotelDTOToEntity(hotel);

        hotelService.deleteHotel(hotelEntity);
    }

    @Override
    public HotelDTO getHotelById(Long hotelId) {
        Hotel hotel = hotelService.getHotelById(hotelId);

        HotelDTO hotelDTO = AdaptersHelper.hotelEntityToDTO(hotel);

        return hotelDTO;
    }

    @Override
    public List<HotelDTO> findAllHotels() {
        List<Hotel> list = hotelService.findAllHotels();
        List<HotelDTO> listDto = new ArrayList<HotelDTO>();
        for (Hotel h : list) {
            listDto.add(AdaptersHelper.hotelEntityToDTO(h));
        }

        return listDto;
    }

    @Override
    public List<HotelDTO> findHotelsByName(String name) {
        List<Hotel> list = hotelService.findHotelsByName(name);
        List<HotelDTO> listDto = new ArrayList<HotelDTO>();
        for (Hotel h : list) {
            listDto.add(AdaptersHelper.hotelEntityToDTO(h));
        }

        return listDto;
    }

    @Override
    public List<HotelDTO> findHotelsByCity(String city) {
        List<Hotel> list = hotelService.findHotelsByCity(city);
        List<HotelDTO> listDto = new ArrayList<HotelDTO>();
        for (Hotel h : list) {
            listDto.add(AdaptersHelper.hotelEntityToDTO(h));
        }

        return listDto;
    }

    public void setHotelService(HotelService hotelService) {
        this.hotelService = hotelService;
    }
}
